﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TrackingAssist.WebTrends
{
    public class XmlSpec:IWebTrendsSpec
    {
        public void LoadSpecFromFile(string fileName, out IList<WebTrends> specAtlasList)
        {
            specAtlasList = new List<WebTrends>();
            
            if (string.IsNullOrEmpty(fileName))
            {
                //return null;
                return;
            }
            
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);

                XmlNodeList atlasNodes = xmlDoc.SelectNodes("//items/item[@type='atlas']");
                if (atlasNodes != null && atlasNodes.Count > 0)
                {
                    foreach (XmlNode node in atlasNodes)
                    {
                        WebTrends webtrends = new WebTrends();
                        webtrends.Id = node.Attributes["id"] == null ? string.Empty : node.Attributes["id"].Value.Trim();
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.Action, node.Attributes["action"] == null ? string.Empty : node.Attributes["action"].Value);
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.ActionName, node.Attributes["actionname"] == null ? string.Empty : node.Attributes["actionname"].Value);
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.Section, node.Attributes["section"] == null ? string.Empty : node.Attributes["section"].Value);
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.Location, node.Attributes["location"] == null ? string.Empty : node.Attributes["location"].Value);
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.Var1, node.Attributes["var1"] == null ? string.Empty : node.Attributes["var1"].Value);
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.Var2, node.Attributes["var2"] == null ? string.Empty : node.Attributes["var2"].Value);
                        WebTrendsSpecUtility.SetSpecAtlasItem(webtrends.Var3, node.Attributes["var3"] == null ? string.Empty : node.Attributes["var3"].Value);
                        webtrends.Description = node.Attributes["description"] == null ? string.Empty : node.Attributes["description"].Value.Trim();
                        specAtlasList.Add(webtrends);
                    }
                }
                //return specAtlasList;


            }
            catch (Exception ex)
            {
                //return null;
                return;
            }
        }
    }
}
